package action;

import beans.ExamsFacadeLocal;
import beans.LinkCatalogueFacadeLocal;
import beans.NewsFacadeLocal;
import com.opensymphony.xwork2.ActionSupport;
import components.Grade;
import entities.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import services.PageDetails;

/**
 *
 * @author corneliugosa
 */
public class ButtonAction extends ActionSupport implements ServletRequestAware{
    ExamsFacadeLocal examsFacade = lookupExamsFacadeLocal();
    NewsFacadeLocal newsFacade = lookupNewsFacadeLocal();
    LinkCatalogueFacadeLocal linkCatalogueFacade = lookupLinkCatalogueFacadeLocal();

    private String pressedButton;
    private HttpServletRequest request;
    private PageDetails pageDetails;
    private List<Catalogue> catalogue;
    private User user;
    private List<Grade> grades;
    private List<News> news;
    private List<Exams> exams;
    private String examId;
    private String message;
    
    public String home(){
        HttpSession session = request.getSession();
        pressedButton = "home";
        pageDetails = (PageDetails)session.getAttribute("pageDetails");       
        pageDetails.setButtonPageDisplay(pressedButton);  
        session.setAttribute("pageDetails", pageDetails);
        session.setAttribute("pressedButton", pressedButton);
        return SUCCESS;
    }
    
    public String homeS(){
        HttpSession session = request.getSession();
        pressedButton = "homeS";
        pageDetails = (PageDetails)session.getAttribute("pageDetails");       
        pageDetails.setButtonPageDisplay(pressedButton);  
        session.setAttribute("pageDetails", pageDetails);
        session.setAttribute("pressedButton", pressedButton);
        return SUCCESS;
    }
    
    public String timetable(){
        HttpSession session = request.getSession();
        pageDetails = (PageDetails)session.getAttribute("pageDetails");
        pressedButton = "not-implemented";
        pageDetails.setButtonPageDisplay(pressedButton);
        session.setAttribute("pageDetails", pageDetails);
        session.setAttribute("pressedButton", pressedButton);
        return SUCCESS;
    }
    
    public String examsSchedule(){
        HttpSession session = request.getSession();
        pageDetails = (PageDetails)session.getAttribute("pageDetails");
        pressedButton = "exams";
        pageDetails.setButtonPageDisplay(pressedButton);
        user = (User)session.getAttribute("user");
        
        exams = examsFacade.findByClass(user.getPerson().getClasses().get(0));
        
        session.setAttribute("exams", exams);
        session.setAttribute("pageDetails", pageDetails);
        session.setAttribute("pressedButton", pressedButton);
        return SUCCESS;
    }
    
    public String grades(){
        HttpSession session = request.getSession();
        pressedButton = "grades";
        grades = new ArrayList<Grade>();
        
        pageDetails = (PageDetails)session.getAttribute("pageDetails");       
        user = (User)session.getAttribute("user");
        catalogue = linkCatalogueFacade.findCatalogueByPersonId(user.getPerson());
        if(catalogue!=null){
            for(Catalogue cat:catalogue){
                Grade grade = new Grade(cat.getIdSubjectMatter().getName(), cat.getGrade());
                grades.add(grade);
            }
        }
        
        pageDetails.setButtonPageDisplay(pressedButton);  
        session.setAttribute("pageDetails", pageDetails);
        session.setAttribute("pressedButton", pressedButton);
        session.setAttribute("grades", grades);
        return SUCCESS;
    }
    
    public String news(){
        HttpSession session = request.getSession();
        pressedButton = "news";
        pageDetails = (PageDetails)session.getAttribute("pageDetails"); 
        pageDetails.setButtonPageDisplay(pressedButton); 

        news = newsFacade.findAll();
        if(news==null || news.get(0)==null){
            news.add(new News(0 ,"There are no news to display.",""));
        }
        
        session.setAttribute("news", news);
        session.setAttribute("pageDetails", pageDetails);
        session.setAttribute("pressedButton", pressedButton);
        return SUCCESS;
    }
    
    public String newsS(){
        return SUCCESS;
    }
    
    public String changePass(){
        HttpSession session = request.getSession();
        pageDetails = (PageDetails)session.getAttribute("pageDetails");
        pressedButton = "changePass"; 
        pageDetails.setButtonPageDisplay(pressedButton);
        session.setAttribute("pageDetails", pageDetails);
        session.setAttribute("pressedButton", pressedButton);
        return SUCCESS;
    }
    public String homeP(){
        HttpSession session = request.getSession();
        pageDetails = (PageDetails)session.getAttribute("pageDetails");
        user = (User)session.getAttribute("user");
        pressedButton = "homeP"; 
        
        List<Exams> exams = user.getPerson().getExamsList();
        
        pageDetails.setButtonPageDisplay(pressedButton);
        session.setAttribute("exams", exams);
        session.setAttribute("pageDetails", pageDetails);
        session.setAttribute("pressedButton", pressedButton);
        return SUCCESS;
    }
    
    public String examsP(){      
        HttpSession session = request.getSession();
        pageDetails = (PageDetails)session.getAttribute("pageDetails");
        user = (User)session.getAttribute("user");
        pressedButton = "examsP"; 
        
        List<Exams> exams = examsFacade.findByPerson(user.getPerson());
        
        pageDetails.setButtonPageDisplay(pressedButton);
        session.setAttribute("exams", exams);
        session.setAttribute("pageDetails", pageDetails);
        session.setAttribute("pressedButton", pressedButton);
        return SUCCESS;
    }
    
    public String timetableP(){
        HttpSession session = request.getSession();
        pageDetails = (PageDetails)session.getAttribute("pageDetails");
        pressedButton = "changePass"; 
        pageDetails.setButtonPageDisplay(pressedButton);
        session.setAttribute("pageDetails", pageDetails);
        session.setAttribute("pressedButton", pressedButton);
        return SUCCESS;
    }
    
    public String timetableS(){
        return SUCCESS;
    }
    
    public String classes(){
        HttpSession session = request.getSession();
        pageDetails = (PageDetails)session.getAttribute("pageDetails");
        pressedButton = "classesP"; 
        pageDetails.setButtonPageDisplay(pressedButton);
        session.setAttribute("pageDetails", pageDetails);
        session.setAttribute("pressedButton", pressedButton);
        return SUCCESS;
    }
    
    public String scheduleExam(){
        action.ScheduleExam schedule = new action.ScheduleExam();
        HttpSession session = request.getSession();
        pageDetails = (PageDetails)session.getAttribute("pageDetails");
        user = (User)session.getAttribute("user");
        pressedButton = "scheduleExam"; 
        pageDetails.setButtonPageDisplay(pressedButton);
        
        List<entities.Class> classes = user.getPerson().getClasses();
        Set<SubjectMatter> subjectMatterList = user.getPerson().getSubjectMatterList();
        
        schedule.setClassesList(classes);
        schedule.setSubjectMatterList(subjectMatterList);

        session.setAttribute("schedule", schedule);
        session.setAttribute("pageDetails", pageDetails);
        session.setAttribute("pressedButton", pressedButton);
        return SUCCESS;
    }
    
    public String examRequests(){
        System.out.println("Exam req");
        HttpSession session = request.getSession();
        pageDetails = (PageDetails)session.getAttribute("pageDetails");
        user = (User)session.getAttribute("user");
        pressedButton = "examsS"; 
        return "examRequests";
    }
    
    public String find(){
        return SUCCESS;
    }
    
    public String approve(){
        HttpSession session = request.getSession();
        Exams exam = examsFacade.findById(Integer.parseInt(examId));
        exam.setStatus(1);
        examsFacade.edit(exam);
        
        session.setAttribute("examStat", "approved");
        message = "The exam request has been approved!";
        
        try {
            Context jndiContext = new InitialContext();
            ConnectionFactory connectionFactory = (ConnectionFactory) jndiContext.lookup("jms/ExamResponseFactory");
            Queue queue = (Queue) jndiContext.lookup("jms/ExamResponse");
            Connection connection = connectionFactory.createConnection();

            Session sess = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageProducer messageProducer = sess.createProducer(queue);

            ObjectMessage message = sess.createObjectMessage();
            System.out.println("Exam request has been approved in the approve() method!");
            message.setObjectProperty("code",101);
            
            messageProducer.send(message);
            messageProducer.close();
            connection.close();

        } catch (NamingException ex) {
            Logger.getLogger(ButtonAction.class.getName()).log(Level.SEVERE, null, ex);
        } catch (JMSException ex) {
            ex.printStackTrace();
        }
        
        return SUCCESS;
    }
    
    public String reject(){
        HttpSession session = request.getSession();
        Exams exam = examsFacade.findById(Integer.parseInt(examId));
        exam.setStatus(2);
        examsFacade.edit(exam);
        
        session.setAttribute("examStat", "rejected");
        message = "The exam request has been rejected!";
        System.out.println(message);
        return SUCCESS;
    }
    
    public String operations(){
        return SUCCESS;
    }

    public String getPressedButton(){
        return pressedButton;
    }

    public void setPressedButton(String pressedButton) {
        this.pressedButton = pressedButton;
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    public String getExamId() {
        return examId;
    }

    public void setExamId(String examId) {
        this.examId = examId;
    }

    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    private LinkCatalogueFacadeLocal lookupLinkCatalogueFacadeLocal() {
        try {
            Context c = new InitialContext();
            return (LinkCatalogueFacadeLocal) c.lookup("java:global/School/School-ejb/LinkCatalogueFacade!beans.LinkCatalogueFacadeLocal");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    public List<Catalogue> getCatalogue() {
        return catalogue;
    }

    public void setCatalogue(List<Catalogue> catalogue) {
        this.catalogue = catalogue;
    }

    public LinkCatalogueFacadeLocal getLinkCatalogueFacade() {
        return linkCatalogueFacade;
    }

    public void setLinkCatalogueFacade(LinkCatalogueFacadeLocal linkCatalogueFacade) {
        this.linkCatalogueFacade = linkCatalogueFacade;
    }

    public PageDetails getPageDetails() {
        return pageDetails;
    }

    public void setPageDetails(PageDetails pageDetails) {
        this.pageDetails = pageDetails;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public List<Grade> getGrades() {
        return grades;
    }

    public void setGrades(List<Grade> grades) {
        this.grades = grades;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public List<Exams> getExams() {
        return exams;
    }

    public void setExams(List<Exams> exams) {
        this.exams = exams;
    }

    public List<News> getNews() {
        return news;
    }

    public void setNews(List<News> news) {
        this.news = news;
    }

    private NewsFacadeLocal lookupNewsFacadeLocal() {
        try {
            Context c = new InitialContext();
            return (NewsFacadeLocal) c.lookup("java:global/School/School-ejb/NewsFacade!beans.NewsFacadeLocal");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private ExamsFacadeLocal lookupExamsFacadeLocal() {
        try {
            Context c = new InitialContext();
            return (ExamsFacadeLocal) c.lookup("java:global/School/School-ejb/ExamsFacade!beans.ExamsFacadeLocal");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
        
}
